
layui.config({
    base: '/home/js/util/'
}).use(['element','laypage','form','menu','jquery','layer'],function(){
    element = layui.element,laypage = layui.laypage,form = layui.form,menu = layui.menu,$ = layui.jquery,layer = layui.layer;

    //判断是否有用户登录
    var checkLoginState = function () {

        var userInfo = window.sessionStorage.getItem("home-member");
        if (userInfo == null || userInfo == '' || userInfo == undefined){

            return false;
        }
        return true;
    };

    $('#up-post').on('click',function () {

        if (!checkLoginState()){

            layer.msg('您还没登录,登录后重试！', {icon: 2},function () {
                window.location.href = '/home/member-login.html';
            });
            return false;
        }

        layer.open({
            type: 2,
            title: '发帖',
            shadeClose: true,
            shade: false,
            maxmin: true, //开启最大化最小化按钮
            area: ['600px', '400px'],
            content: 'edit-post.html'
        });

    });

    var searchParam = {
        req: {
        },
        page: 1,
        limit: 10,
    };

    var loadPostInfo = function(){

        $.ajax({
            url:'../post-info/postList',
            type:'POST',
            async:true,
            data: JSON.stringify(searchParam),
            timeout:5000,    //超时时间
            dataType:'json',    //返回的数据格式：json/xml/html/script/jsonp/text
            contentType: "application/json",
            success:function(res){
                if (res.code == '0'){
                    renderData(res.data.records);
                    laypageTable(res.data.total);
                }else {
                    layer.msg("获取贴吧信息失败！");
                }
            }
        });
    };

    var laypageTable = function (total) {
        if (total == 0){
            return;
        }
        laypage.render({
            elem: 'demo',
            count: total,
            limit: searchParam.limit,
            curr: searchParam.page,
            layout: ['count', 'prev', 'page', 'next', 'limit', 'refresh', 'skip'],
            jump: function (obj, first) {
                if (!first) {
                    searchParam.page = obj.curr;
                    searchParam.limit = obj.limit;
                    loadPostInfo();
                }
            }
        });
    };

    var renderData = function(data){
        if (data == null || data == '' || data == undefined){
            return;
        }
        var html = '';
        $.each(data,function (index,value) {
            html += ' <div class="item-box">\n' +
                '          <div class="item">\n' +
                '            <div class="whisper-title">\n' +
                '              <i class="layui-icon layui-icon-date"></i><span class="hour">'+value.postDataTime.split(" ")[1]+'</span><span class="date">'+value.postDataTime.split(" ")[0]+'</span><span class="name">---'+value.userName+'</span>\n' +
                '            </div>\n' +
                '            <p class="text-cont">\n' +
                value.postContent      +
                '            </p>\n' +
                '            <div class="op-list">\n' +
                '              <p id="'+ value.postId +'" class="like"><i class="layui-icon layui-icon-praise"></i><span>1200</span></p>\n' +
                '              <p class="edit"><i class="layui-icon layui-icon-reply-fill"></i><span>1200</span></p>\n' +
                '              <p name="switch" class="off"><span>展开</span><i class="layui-icon layui-icon-down"></i></p>\n' +
                '            </div>\n' +
                '          </div>\n' +
                '          <div class="review-version layui-hide">\n' +
                '              <div class="form">\n' +
                '                <img src="/home/img/header2.png">\n' +
                '                <form class="layui-form" action="">\n' +
                '                  <div class="layui-form-item layui-form-text">\n' +
                '                    <div class="layui-input-block">\n' +
                '                      <textarea name="desc" class="layui-textarea"></textarea>\n' +
                '                    </div>\n' +
                '                  </div>\n' +
                '                  <div class="layui-form-item">\n' +
                '                    <div class="layui-input-block" style="text-align: right;">\n' +
                '                      <button class="layui-btn definite">確定</button>\n' +
                '                    </div>\n' +
                '                  </div>\n' +
                '                </form>\n' +
                '              </div>\n' +
                '              <div class="list-cont">\n' +
                '                <div class="cont">\n' +
                '                  <div class="img">\n' +
                '                    <img src="/home/img/header.png" alt="">\n' +
                '                  </div>\n' +
                '                  <div class="text">\n' +
                '                    <p class="tit"><span class="name">吳亦凡</span><span class="data">2020/06/06</span></p>\n' +
                '                    <p class="ct">敢问大师，师从何方？上古高人呐逐一地看完你的作品后，我的心久久 不能平静！这世间怎么可能还有如此精辟的作品？我不敢相信自己的眼睛。自从改革开放以后，我就以为再也不会有任何作品能打动我，没想到今天看到这个如此精妙绝伦的作品好厉害！</p>\n' +
                '                  </div>\n' +
                '                </div>\n' +
                '                <div class="cont">\n' +
                '                  <div class="img">\n' +
                '                    <img src="/home/img/header.png" alt="">\n' +
                '                  </div>\n' +
                '                  <div class="text">\n' +
                '                    <p class="tit"><span class="name">吳亦凡</span><span class="data">2020/06/06</span></p>\n' +
                '                    <p class="ct">敢问大师，师从何方？上古高人呐逐一地看完你的作品后，我的心久久 不能平静！这世间怎么可能还有如此精辟的作品？我不敢相信自己的眼睛。自从改革开放以后，我就以为再也不会有任何作品能打动我，没想到今天看到这个如此精妙绝伦的作品好厉害！</p>\n' +
                '                  </div>\n' +
                '                </div>\n' +
                '                <div class="cont">\n' +
                '                  <div class="img">\n' +
                '                    <img src="/home/img/header.png" alt="">\n' +
                '                  </div>\n' +
                '                  <div class="text">\n' +
                '                    <p class="tit"><span class="name">吳亦凡</span><span class="data">2020/06/06</span></p>\n' +
                '                    <p class="ct">敢问大师，师从何方？上古高人呐逐一地看完你的作品后，我的心久久 不能平静！这世间怎么可能还有如此精辟的作品？我不敢相信自己的眼睛。自从改革开放以后，我就以为再也不会有任何作品能打动我，没想到今天看到这个如此精妙绝伦的作品好厉害！</p>\n' +
                '                  </div>\n' +
                '                </div>\n' +
                '              </div>\n' +
                '          </div>\n' +
                '        </div>';
        });

        var form = '<div class="review-version">\n' +
            '              <div class="form">\n' +
            '                <img src="/home/img/header2.png">\n' +
            '                <form class="layui-form" action="">\n' +
            '                  <div class="layui-form-item layui-form-text">\n' +
            '                    <div class="layui-input-block">\n' +
            '                      <textarea name="desc" class="layui-textarea"></textarea>\n' +
            '                    </div>\n' +
            '                  </div>\n' +
            '                  <div class="layui-form-item">\n' +
            '                    <div class="layui-input-block" style="text-align: right;">\n' +
            '                      <button class="layui-btn definite">確定</button>\n' +
            '                    </div>\n' +
            '                  </div>\n' +
            '                </form>\n' +
            '              </div>\n' +
            '          </div>';
        $('#post-list').html(html);

        var flag = false;
        menu.init();
        menu.off();
        menu.submit();
        $("body").on('click','.like',function (e) {
            if (flag){
                $.post("../post-like/unlike/"+e.currentTarget.id);
            }else {
                $.post("../post-like/like/"+e.currentTarget.id);
            }
        })
    };

    loadPostInfo();
})